<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Member;
use App\Account;
use App\Withdraw;
use App\Shop;

Class ShopController extends AdminController{
    
    /**
     * 商户余额
     */
    public function shopAmount(Request $request){
        $member_id = trim($request->input('member_name', ''));
        $company = trim($request->input('company', ''));
        $catid = trim($request->input('catid', ''));
        
        $page=trim($request->input('page', 0));//页码
        $pagenumber=trim($request->input('pagenumber', '30'));//分页个数
        
        $sql = "select  a.member_id,a.member_name, a.company, a.catid, b.total_amount,b.balance_amount, b.freeze_amount from mega_shop as a left join mega_account as b on a.member_id=b.member_id where 1 ";
        $sqlcount = "select count(a.member_name) as count from mega_shop as a left join mega_account as b on a.member_id=b.member_id where 1";
        $param = array();
        $sqlwhere = '';
        if($member_id){
            $sqlwhere.=" and a.member_id=:member_id ";
            $param[':member_id']= $member_id;
        }
        if($company){
            $company = "%$company%";
            $sqlwhere.=" and a.company like :company ";
            $param[':company']= $company;
        }
        if($catid){
            $sqlwhere.=" and a.catid=:catid ";
            $param[':catid']= $catid;
        }
        $start = $page * $pagenumber;
        $limit = " limit $start, $pagenumber ";
        $results = DB::select($sql. $sqlwhere. $limit, $param);
        $count = DB::select($sqlcount.$sqlwhere, $param);
        
        $data = array(
            'count'=>$count[0]->count,
            'items'=>$results,
            'page'=>$page
        );
        
        $this->output($data);
    }
    
    /**
     * 商户对账信息
     */
    public function shopOrders(Request $request){
        $member_id = trim($request->input('member_name', ''));
        $company = trim($request->input('company', ''));
        $catid = trim($request->input('catid', ''));
        
        $page=trim($request->input('page', 0));//页码
        $pagenumber=trim($request->input('pagenumber', '30'));//分页个数
        
        $sql = "select  a.member_id,a.member_name, a.company, a.catid, b.order_id,b.created_at,b.orderpay, b.amount ,b.amount_shop,b.amount_lmj,b.fanxian_amount  from mega_shop as a left join mega_shop_order as b on a.member_id=b.member_id where 1 ";
        $sqlcount = "select count(a.member_name) as count from mega_shop as a left join mega_shop_order as b on a.member_id=b.member_id where 1";
        $param = array();
        $sqlwhere = '';
        if($member_id){
            $sqlwhere.=" and a.member_id=:member_id ";
            $param[':member_id']= $member_id;
        }
        if($company){
            $company = "%$company%";
            $sqlwhere.=" and a.company like :company ";
            $param[':company']= $company;
        }
        if($catid){
            $sqlwhere.=" and a.catid=:catid ";
            $param[':catid']= $catid;
        }
        $start = $page * $pagenumber;
        $limit = " order by b.id desc limit $start, $pagenumber ";
        $results = DB::select($sql. $sqlwhere. $limit, $param);
        $count = DB::select($sqlcount.$sqlwhere, $param);
        
        $data = array(
            'count'=>$count[0]->count,
            'items'=>$results,
            'page'=>$page
        );
        
        $this->output($data);
    }
    
    /**
     * 商铺提现银行卡列表
     * @param Request $request
     */
    public function cardlist(Request $request){
        $member_id = trim($request->input('member_name', ''));
        $company = trim($request->input('company', ''));
        $catid = trim($request->input('catid', ''));
        
        $page=trim($request->input('page', 0));//页码
        $pagenumber=trim($request->input('pagenumber', '30'));//分页个数
        
        $sql = "select  a.member_id,a.member_name, a.company, a.catid, a.cardholder,a.bank_from,a.bank_account from mega_shop as a  where 1 ";
        $sqlcount = "select count(a.member_name) as count from mega_shop as a where 1";
        $param = array();
        $sqlwhere = '';
        if($member_id){
            $sqlwhere.=" and a.member_id=:member_id ";
            $param[':member_id']= $member_id;
        }
        if($company){
            $company = "%$company%";
            $sqlwhere.=" and a.company like :company ";
            $param[':company']= $company;
        }
        if($catid){
            $sqlwhere.=" and a.catid=:catid ";
            $param[':catid']= $catid;
        }
        $start = $page * $pagenumber;
        $limit = " limit $start, $pagenumber ";
        $results = DB::select($sql. $sqlwhere. $limit, $param);
        $count = DB::select($sqlcount.$sqlwhere, $param);
        
        $data = array(
            'count'=>$count[0]->count,
            'items'=>$results,
            'page'=>$page
        );
        
        $this->output($data);
    }
    
    
    
    /**
     * 查单个商户的可提现余额
     */
    public function shopBalanceAmount(Request $request){
        $member_id = trim($request->input('member_id', ''));
        $account = Account::where('member_id', $member_id)->first(); 
        $data = array(
            'amount'=>$account->balance_amount 
        ); 
        $this->output($data);
    }
    
    /**
     * 添加结算申请
     * @param Request $request
     */
    public function shenqingjiesuan(Request $request){
        $member_id = trim($request->input('member_id', ''));
        $amount = sprintf('%.2f', $request->input('amount', '') );
        $account = Account::where('member_id', $member_id)->first(); 
        $shop = Shop::where('member_id',$member_id)->first();
        if( $account->balance_amount >= $amount ){
            //检测余额够不够
            if($amount < 100){
                return phoneReturnError('提现金额不能小于100元');
            }
            $account_result = $account;
            $memberId = $member_id;
            $balance_amount = $account_result['balance_amount'];
            $memberInfo = Member::find($memberId);
            if ($balance_amount >= $amount) {
                $repay_id = updateAccountLog($memberId, $memberInfo['name'], $amount, '提现', 2, 0, 99);
                //扣手续费 #TODO
            } else {
                $this->err('余额不足');
            }
            // 创建提现
            $withDraw = new Withdraw();
            $withDraw->member_id = $memberId;
            $withDraw->amount = $amount;
            $withDraw->fee = 0;
            $withDraw->realname = $shop->cardholder;
            $withDraw->account = $shop->bank_account;
            $withDraw->bank_name = $shop->bank_from;
            $withDraw->status = 1;
            $withDraw->is_shop = 1;
            $withDraw->old_amount = $account->total_amount;
            $withDraw->old_amount_balance = $account->balance_amount;
            if($withDraw->save()){
                $this->output(array(), 200, '申请成功');
            }
            $this->err('提现异常');
        }else{
            $this->err('金额错误，不能超过可提现金额');
        }
    }
    
    /**
     * 商铺提现审核列表
     * @param Request $request
     */
    public function WithdrawShenhe(Request $request){
        $member_id = trim($request->input('member_name', ''));
        $company = trim($request->input('company', ''));
        $catid = trim($request->input('catid', ''));
        
        $page=trim($request->input('page', 0));//页码
        $pagenumber=trim($request->input('pagenumber', '30'));//分页个数
        
        $sql = "select b.id, a.member_id,a.member_name, a.company, a.catid, b.old_amount, b.old_amount_balance, b.amount, b.status from mega_withdraw as b left join mega_shop as a on b.member_id = a.member_id where b.is_shop = 1  ";
        $sqlcount = "select count(a.member_name) as count from mega_withdraw as b left join mega_shop as a on b.member_id = a.member_id where b.is_shop = 1 ";
        $param = array();
        $sqlwhere = '';
        if($member_id){
            $sqlwhere.=" and a.member_id=:member_id ";
            $param[':member_id']= $member_id;
        }
        if($company){
            $company = "%$company%";
            $sqlwhere.=" and a.company like :company ";
            $param[':company']= $company;
        }
        if($catid){
            $sqlwhere.=" and a.catid=:catid ";
            $param[':catid']= $catid;
        }
        $start = $page * $pagenumber;
        $limit = " order by b.id desc limit $start, $pagenumber ";
        $results = DB::select($sql. $sqlwhere. $limit, $param);
        $count = DB::select($sqlcount.$sqlwhere, $param);
        
        $data = array(
            'count'=>$count[0]->count,
            'items'=>$results,
            'page'=>$page
        );
        
        $this->output($data);
    }
    
}